﻿using Common.Device.Enums;
using Common.Device.Models;
using Common.Tool.DataTools;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.Device.Views.Devices
{
    /// <summary>
    /// GeChaoJiCheView.xaml 的交互逻辑
    /// </summary>
    public partial class GeChaoJiCheView : UserControl
    {
        ViewModels.GeChaoJiCheViewModel viewModel = new ViewModels.GeChaoJiCheViewModel();
        public GeChaoJiCheView(DeviceEnum.DeviceModelEnum model, Order order = null)
        {
            InitializeComponent();
            DataContext = viewModel;
            if (order != null)
            {
                //根据测试指令刷新界面
                Enum.TryParse(order.temp, out GeChaoJiEnum.TestMode mode);
                GeChaoJi parameter = JsonConvert.DeserializeObject<GeChaoJi>(order.deviceModel);
                switch (mode)
                {
                    case GeChaoJiEnum.TestMode.初始化:
                        viewModel.MenuIndex = 0;
                        viewModel.IniChannel = parameter.通道;
                        break;
                    case GeChaoJiEnum.TestMode.测试模式:
                        viewModel.MenuIndex = 1;
                        viewModel.TmodeChannel = parameter.通道;
                        break;
                    case GeChaoJiEnum.TestMode.读参数:
                        viewModel.MenuIndex = 2;
                        viewModel.RparaChannel = parameter.通道;
                        viewModel.RparaVMin = parameter.下限值?.ToString();
                        viewModel.RparaVMax = parameter.上限值?.ToString();
                        viewModel.RparaCMin = parameter.下限值1?.ToString();
                        viewModel.RparaCMax = parameter.上限值1?.ToString();
                        break;
                    case GeChaoJiEnum.TestMode.读端口:
                        viewModel.MenuIndex = 3;
                        viewModel.RportChannel = parameter.通道;
                        viewModel.RportTemp = parameter.电平;
                        break;
                    case GeChaoJiEnum.TestMode.测试通讯端口:
                        viewModel.MenuIndex = 4;
                        viewModel.TportChannel = parameter.通道;
                        break;
                    case GeChaoJiEnum.TestMode.测试升降电机:
                        viewModel.MenuIndex = 5;
                        viewModel.TportChannel = parameter.通道;
                        viewModel.RportTemp = parameter.动作类型;
                        break;
                    case GeChaoJiEnum.TestMode.测试行走电机:
                        viewModel.MenuIndex = 6;
                        viewModel.TwalkChannel = parameter.通道;
                        viewModel.TwalkLMin = parameter.下限值?.ToString();
                        viewModel.TwalkLMax = parameter.上限值?.ToString();
                        viewModel.TwalkRMin = parameter.下限值1?.ToString();
                        viewModel.TwalkRMax = parameter.上限值1?.ToString();
                        break;
                    case GeChaoJiEnum.TestMode.测试割草电机:
                        viewModel.MenuIndex = 7;
                        viewModel.TmowChannel = parameter.通道;
                        viewModel.TmowMin = parameter.下限值?.ToString();
                        viewModel.TmowMax = parameter.上限值?.ToString();
                        break;
                    case GeChaoJiEnum.TestMode.测试结束:
                        viewModel.MenuIndex = 8;
                        viewModel.EndChannel = parameter.通道;
                        break;
                    default:
                        break;
                }
            }
        }

        //仪器对象
        public Interfaces.IGeChaoJi Device;
        private void IniClick(object sender, RoutedEventArgs e)
        {
            Models.GeChaoJi parameter = new Models.GeChaoJi()
            {
                mode = GeChaoJiEnum.TestMode.初始化,
                通道 = viewModel.IniChannel,
            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void TmodeClick(object sender, RoutedEventArgs e)
        {
            Models.GeChaoJi parameter = new Models.GeChaoJi()
            {
                mode = GeChaoJiEnum.TestMode.测试模式,
                通道 = viewModel.TmodeChannel,
            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void RparaClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null;
            double? min1 = null; double? max1 = null;
            if (!DataConvert.GetDouble(viewModel.RparaVMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("电压范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.RparaVMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("电压范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.RparaCMax, ref max1))
            {
                View.Controls.Dialogs.TipView.Show("温度范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.RparaCMin, ref min1))
            {
                View.Controls.Dialogs.TipView.Show("温度范围输入有误!");
                return;
            }
            Models.GeChaoJi parameter = new Models.GeChaoJi()
            {
                mode = GeChaoJiEnum.TestMode.读参数,
                通道 = viewModel.TmodeChannel,
                上限值 = max,
                下限值 = min,
                上限值1 = max1,
                下限值1 = min1
            }; 
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("电压范围输入有误!");
                return;
            }
            if (parameter.MinMaxCheck1() == false)
            {
                View.Controls.Dialogs.TipView.Show("温度范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void RportClick(object sender, RoutedEventArgs e)
        {
            Models.GeChaoJi parameter = new Models.GeChaoJi()
            {
                mode = GeChaoJiEnum.TestMode.读端口,
                电平 = viewModel.RportTemp,
                通道 = viewModel.RportChannel,
            };

            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void TportClick(object sender, RoutedEventArgs e)
        {
            Models.GeChaoJi parameter = new Models.GeChaoJi()
            {
                mode = GeChaoJiEnum.TestMode.测试通讯端口,
                通道 = viewModel.TportChannel,
            };

            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void TliftClick(object sender, RoutedEventArgs e)
        {
            Models.GeChaoJi parameter = new Models.GeChaoJi()
            {
                mode = GeChaoJiEnum.TestMode.测试升降电机,
                动作类型 = viewModel.TliftTemp,
                通道 = viewModel.TliftChannel,
            };

            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void TwalkClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null;
            double? min1 = null; double? max1 = null;
            if (!DataConvert.GetDouble(viewModel.TwalkLMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("左轮转速范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.TwalkLMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("左轮转速范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.TwalkRMax, ref max1))
            {
                View.Controls.Dialogs.TipView.Show("右轮转速范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.TwalkRMin, ref min1))
            {
                View.Controls.Dialogs.TipView.Show("右轮转速范围输入有误!");
                return;
            }
            Models.GeChaoJi parameter = new Models.GeChaoJi()
            {
                mode = GeChaoJiEnum.TestMode.测试行走电机,
                通道 = viewModel.TwalkChannel,
                动作类型 = viewModel.TwalkTemp,
                上限值 = max,
                下限值 = min,
                上限值1 = max1,
                下限值1 = min1
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("电压范围输入有误!");
                return;
            }
            if (parameter.MinMaxCheck1() == false)
            {
                View.Controls.Dialogs.TipView.Show("温度范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void TmowClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.TmowMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("电机转速范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.TmowMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("电机转速范围输入有误!");
                return;
            }
            
            Models.GeChaoJi parameter = new Models.GeChaoJi()
            {
                mode = GeChaoJiEnum.TestMode.测试割草电机,
                通道 = viewModel.TwalkChannel,
                动作类型 = viewModel.TmowTemp,
                上限值 = max,
                下限值 = min
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("电机转速范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void EndClick(object sender, RoutedEventArgs e)
        {
            Models.GeChaoJi parameter = new Models.GeChaoJi()
            {
                mode = GeChaoJiEnum.TestMode.测试结束,
                通道 = viewModel.EndChannel,
            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }
    }
}
